package startup

import (
	"gitee.com/cristiane/micro-mall-search-order-consumer/vars"
	"log"
	"strings"
	es7 "github.com/elastic/go-elasticsearch/v7"
)

// SetupVars 加载变量
func SetupVars() error {
	var err error
	cfg := es7.Config{
		Addresses: strings.Split(vars.ElasticsearchSetting.Addresses, ","),
		Username:  vars.ElasticsearchSetting.Username,
		Password:  vars.ElasticsearchSetting.Password,
	}
	vars.ElasticsearchClient, err = es7.NewClient(cfg)
	if err != nil {
		log.Fatalf("Config elasticsearch err: %v", err)
	}
	res, err := vars.ElasticsearchClient.Info()
	if err != nil {
		log.Fatalf("Load elasticsearch info response err: %s", err)
	}
	defer res.Body.Close()
	if res.IsError() {
		log.Fatalf("Elasticsearch error: %s", res.String())
	}
	log.Printf("Elasticsearch info: %+v", res)
	log.Printf("Config elasticsearch ok")
	return err
}

func StopFunc() error {
	var err error
	// 当kelvins收到退出信号时将会调用
	// 本应用的资源回收通常在这里进行，kelvins自动加载的资源回收由框架进行
	return err
}
